package com.homedo.odin.solution.service;

import com.homedo.core.common.base.service.BaseService;
import com.homedo.core.common.exception.BizException;
import com.homedo.core.common.util.BeanUtils;
import com.homedo.odin.solution.contract.mng.DictDataResp;
import com.homedo.odin.solution.dao.DictDataMapper;
import com.homedo.odin.solution.enumcode.basic.ErrorCodeEnum;
import com.homedo.odin.solution.model.dto.SensitiveWord;
import com.homedo.odin.solution.model.entity.DictData;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;

/**
 * <p> 字典数据 服务实现类 </p>
 *
 * @author HarveyDing
 * @since 2018-03-08
 */
@Service
public class DictDataServiceImpl extends BaseService<DictDataMapper, DictData> {

    /**
     * 根据类型查找字典值列表
     */
    public List<String> findValueListByType(String type) {
        return baseMapper.findValueListByType(type);
    }

    /**
     * 根据类型查找字典
     */
    public List<SensitiveWord> findSensitiveWord(String type, String value) {
        return baseMapper.findSensitiveWord(type, value);
    }

    /**
     * 修改值
     *
     * @param original 原值
     * @param target 修改之后的值
     * @param type 类型
     */
    public Integer updateValueByValueAndType(Long userId, String original, String target, String type,
            String userName) {
        return baseMapper.updateValueByUserIdAndType(userId, original, target, type, userName);
    }

    public Integer updateStatusByValueAndType(String value, Long userId, String type, int status, String userName) {
        return baseMapper.updateStatusByValueAndType(value, userId, type, status, userName);
    }

    public DictData getDictDataById(@Param("id") Integer id) {
        return baseMapper.getDictDataById(id);
    }

    public DictData getDictDataByTypeAndKey(String type, String key) {
        return baseMapper.getDictDataByTypeAndKey(type, key);
    }

    public DictDataResp getPcDictDataByTypeAndKey(String type, String key) {
        DictData dictData = baseMapper.getDictDataByTypeAndKey(type, key);
        if (dictData == null) {
            throw new BizException(ErrorCodeEnum.NotExist.getDbValue(), "该数据不存在");
        }
        DictDataResp resp = BeanUtils.transfrom(DictDataResp.class, dictData);
        return resp;
    }
}
